package auto;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.swing.JOptionPane;

public class Metode {
	public static boolean validno = false;
		
	// ***********************

		public void validacija(String user, String pass, LogIn li) {
			try {
				Class.forName(masb.driverMySql);
				Connection conn = DriverManager.getConnection(masb.mySqlString);
				String passw = null;
				String Prezime = null;
				String Ime = null;
				int uloga = 0;
				int id = 0;
			
				Statement st = conn.createStatement();
				ResultSet rs = st
						.executeQuery("SELECT Pass, Prezime, Ime, UlogaID, idKorisnika FROM pKorisnik WHERE (UserName = '"
								+ user + "')");
				if (rs.next()) {
					passw = rs.getString(1);
					if (masb.comp(pass.compareTo(passw))) {
						validno = true;
						Prezime = masb.stringNull(rs.getObject(2));
						Ime = masb.stringNull(rs.getObject(3));
						uloga = masb.intNull(rs.getObject(4));
						id = rs.getInt(5);
						rs.close();
						String str = "INSERT INTO pLogging (KorisnikID, DatumVreme) VALUES ("
								+ id + ",now())";
						st.executeUpdate(str);
						pocniSmenu(id);
					}
				}
				conn.close();
				li.podaci(validno, id, Prezime, Ime, uloga);
			} catch (ClassNotFoundException e) {
				if (masb.admin)
					JOptionPane.showMessageDialog(null, e.getMessage());
			} catch (Exception e) {
				if (masb.admin)
					JOptionPane.showMessageDialog(null, e.getMessage());
			}
		}

		// ***********************

		private static void uzmiCene(){
			try{
				Class.forName(masb.driverMySql);
				Connection conn = DriverManager.getConnection(masb.mySqlString);
				Statement st = conn.createStatement();
				ResultSet rs = st.executeQuery("SELECT FROM Cene WHERE (Nevazece <> 1)");
				if (rs.next()){
					
				}
			}
			catch (Exception e) {
				JOptionPane.showMessageDialog(null, e.toString());
			}
		}
		
		// **********************

		private static void pocniSmenu(int korisnikID) {
			try {
				Class.forName(masb.driverMySql);
				Connection conn = DriverManager.getConnection(masb.mySqlString);
				Statement st = conn.createStatement();
				st.executeUpdate("INSERT INTO pSmena (KorisnikID,Datum,Vreme,Mesto) VALUES ("
						+ korisnikID + ",now(),now(),'Beograd')");
				conn.close();
			} catch (Exception e) {
				if (masb.admin)
					JOptionPane.showMessageDialog(null, e.toString());
			}
		}

		// **********************

	
}
